<template>
	<view>
		<view class="uni-margin-wrap">
			<!-- search组件 -->
			<view class="search-box">
				<my_search @gotoSearch="gotoSearch"></my_search>
			</view>
			<!-- swiper -->
			<swiper class="swiper" circular indicator-dots autoplay interval="2000">
				<swiper-item v-for="item in swiperList" :key="item.goods_id">
					<navigator class="swiper-item" :url="'/subpkg/goods_detail/goods_detail?goos_id='+item.goods_id">
						<img :src="item.image_src" alt="">
					</navigator>
				</swiper-item>
			</swiper>
			<!-- 分类导航区域 -->
			<view class="nav-list">
				<view class="nav-item" v-for="(item, index) in navList" :key="index"
					@click="navClickHandler(item.name)">
					<image :src="item.image_src" class="nav-img"></image>
				</view>
			</view>
			<!-- 楼层区域 -->
			<view class="floor_list">
				<view class="floor_item" v-for="(item,index) in floorList" :key="index">
					<!-- 楼层标题 -->
					<image :src="item.floor_title.image_src" class="floot_title"></image>
					<view class="floor_img_box">
						<navigator class="left_img_box" :url="item.product_list[0].navigator_url">
							<image :src="item.product_list[0].image_src"
								:style="{width:item.product_list[0].image_width+'rpx'}" mode="widthFix"></image>
						</navigator>
						<view class="right_img_box">
							<navigator class="right_img_item" v-for="(item2, i2) in item.product_list" :key="i2" v-if="i2!=0" :url="item2.navigator_url">
								<image :src="item2.image_src" :style="{width: item2.image_width + 'rpx'}"
									mode="widthFix"></image>
							</navigator>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: 'Home',
		data() {
			return {
				swiperList: [],
				// 分类导航数据列表
				navList: [],
				// 楼层数据
				floorList: []
			};
		},
		onLoad() {
			this.getSwiperList()
			this.getNavList()
			this.getFloorList()
		},
		methods: {
			// 获取swiper数据
			async getSwiperList() {
				let {
					data: res
				} = await uni.$http.get('/api/public/v1/home/swiperdata')
				if (res.meta.status == 200) {
					this.swiperList = res.message
				} else uni.$showMsg()
			},
			// 获取分类数据
			async getNavList() {
				let {
					data: res
				} = await uni.$http.get('/api/public/v1/home/catitems')
				if (res.meta.status == 200) {
					this.navList = res.message
				} else uni.$showMsg()
			},
			// 点击切换tabBar的页面
			navClickHandler(name) {
				if (name === '分类') {
					uni.switchTab({
						url: '/pages/cate/cate'
					})
				}
			},
			// 获取楼层数据
			async getFloorList() {
				let {
					data: res
				} = await uni.$http.get('/api/public/v1/home/floordata')
				if (res.meta.status == 200) {
					// 处理navigator_url数据
					res.message.forEach((i1) => {
						i1.product_list.forEach((i2) => {
							i2.navigator_url = '/subpkg/goods_list/goods_list?' + i2.navigator_url
								.split('?')[1]
						})
					})
					this.floorList = res.message
				}
			},
			// 搜索框的自定义事件
			gotoSearch(){
				uni.navigateTo({
					url:'/subpkg/search/search'
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.search-box{
		position: sticky;
		top:0;
		z-index: 99;
	}
	.swiper {
		height: 330rpx;

		.swiper-item,
		image {
			width: 100%;
			height: 100%;
		}
	}

	.nav-list {
		display: flex;
		justify-content: space-around;
		margin: 15px 0;

		.nav-img {
			width: 128rpx;
			height: 140rpx;
		}
	}

	.floot_title {
		height: 60rpx;
		width: 100%;
		display: flex;


	}

	.right_img_box {
		display: flex;
		flex-wrap: wrap;
		justify-content: space-around;
	}

	.floor_img_box {
		display: flex;
		padding-left: 10rpx;
	}
	
</style>
